<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="标题名称:">
        <el-input class="selectWidth" v-model="dataForm.key" placeholder="标题名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="产品编号:">
        <el-input class="selectWidth" v-model="dataForm.code" placeholder="产品编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="学期学年:">
        <el-select class="selectWidth" v-model="dataForm.studyId" placeholder="学期学年">
          <el-option v-for="item in studyOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程类型:">
        <el-select class="selectWidth" v-model="dataForm.type" placeholder="课程类型">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上线状态:">
        <el-select class="selectWidth" v-model="dataForm.status" placeholder="上线状态">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-dropdown @command="handleCommand" style="margin-left: 10px;">
          <el-button type="primary">
            更多操作<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="plsc">批量删除</el-dropdown-item>
            <el-dropdown-item command="plsx">批量上线</el-dropdown-item>
            <el-dropdown-item command="plxx">批量下线</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <el-table ref="tableRef" :data="dataList" :height="height" :header-cell-style="tableHeaderStyle" border
      v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>

      <el-table-column prop="name" header-align="center" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="分类">
        <template slot-scope="scope">
          <span v-if="Number(scope.row.type) === 0">专业科目</span>
          <span v-else size="small">公需科目</span>
        </template>
      </el-table-column>
      <el-table-column prop="credit" header-align="center" align="center" label="学时">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="发布日期">
      </el-table-column>

      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0">已下线</span>
          <span v-else-if="scope.row.status === 1" size="small">已上线</span>
          <span v-else-if="scope.row.status === 2" size="small">已删除</span>

        </template>
      </el-table-column>

      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './manage-add-or-update.vue'
export default {
  data() {
    return {
      tableHeaderStyle: { background: '#f5f7fa !important', height: '40px !important', padding: '4px !important' },

      height: 0,
      dataForm: {
        key: '',
        status: '',
        studyId: '',
        code: '',
        type: '',

      },
      studyOptions: [
        {
          value: '',
          label: '请选择'
        },
      ],
      typeOptions: [
        {
          value: '',
          label: '请选择'
        }, {
          value: 0,
          label: '专业科目'
        }, {
          value: 1,
          label: '公需科目'
        }
      ],
      statusOptions: [
        {
          value: '',
          label: '全部'
        }, {
          value: 1,
          label: '已上线'
        }, {
          value: 0,
          label: '已下线'
        }
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getstudyOptionsList()
    this.getDataList()
  },
  mounted() {
    console.log(document.body.clientHeight)
    // 至于减去多少，这个要看你的页面高度和你想将滚动轴固定在哪里了，数值可以更换
    this.$nextTick(() => {
      let tableHeight = document.body.clientHeight - 335
      this.height = tableHeight
    })
  },
  methods: {
    //获取学期学年数据
    getstudyOptionsList() {
      this.$http({
        url: this.$http.adornUrl('/class/study/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 9999999
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          if (data.page.list && data.page.list.length > 0) {
            for (let i = 0; i < data.page.list.length; i++) {
              this.studyOptions.push({
                value: data.page.list[i].id,
                label: data.page.list[i].name
              })
            }
          }
        }
      })
    },
    repairTable() {
      this.$nextTick(() => {
        // _self.$refs.你的el-table的ref.doLayout()
        this.$refs.tableRef.doLayout()
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let searchParams = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key,
        status: this.dataForm.status,
        studyId: this.dataForm.studyId,
        code: this.dataForm.code,
        type: this.dataForm.type
      }
      this.$http({
        url: this.$http.adornUrl('/class/manage/list'),
        method: 'get',
        params: this.$http.adornParams(searchParams)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.repairTable()
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      console.log(val)
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
            url: this.$http.adornUrl('/class/manage/bachUpdateStatus'),
            method: 'post',
            data: this.$http.adornData({
              'ids': ids,
              'status': 2
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      }).catch(() => { })
    },
    // 更多操作
    handleCommand(command) {
      if (this.dataListSelections.length === 0) {
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning',
          duration: 1500
        })
        return
      }
      const ids = this.dataListSelections.map(item => item.id)
      if (command == 'plsc') {
        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/class/manage/bachUpdateStatus'),
            method: 'post',
            data: this.$http.adornData({
              'ids': ids,
              'status': 2
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      } else if (command == 'plsx') {
        this.$confirm(`确定进行上线操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/class/manage/bachUpdateStatus'),
            method: 'post',
            data: this.$http.adornData({
              'ids': ids,
              'status': 1
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      } else if (command == 'plxx') {
        this.$confirm(`确定进行下线操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/class/manage/bachUpdateStatus'),
            method: 'post',
            data: this.$http.adornData({
              'ids': ids,
              'status': 0
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
}
</script>
